﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using QLNhaHangTiecCuoi.Models;
using System.Threading.Tasks;

namespace QLNhaHangTiecCuoi.Controllers
{
    public class DichVusController : Controller
    {
        private QLNhaHangTCEntities db = new QLNhaHangTCEntities();

        // GET: /DichVus/
        public ActionResult Index()
        {
            return View(db.DichVus.ToList());
        }

        // GET: /DichVus/Details/5
        public ActionResult Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DichVu dichvu = db.DichVus.Find(id);
            if (dichvu == null)
            {
                return HttpNotFound();
            }
            return View(dichvu);
        }
        public string CreateMaDV()
        {
            var item = db.DichVus.OrderByDescending(i => i.MaDV).FirstOrDefault();
            string ma;
            if (item == null) { ma = "DV0000"; }
            else ma = item.MaDV;
            int so = Convert.ToInt32(ma.Substring(2, 4));
            so++;
            string MaDV = "DV" + so.ToString("0000");
            return MaDV;
        }
        // GET: /DichVus/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /DichVus/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "TenDV,MoTa,Gia")] DichVu dichvu)
        {
            DichVu DV = new DichVu()
            {
                MaDV = CreateMaDV(),
                TenDV = dichvu.TenDV,
                MoTa = dichvu.MoTa,
                Gia = dichvu.Gia
            };
            if (ModelState.IsValid)
            {
                db.DichVus.Add(DV);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(dichvu);
        }

        // GET: /DichVus/Edit/5
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DichVu dichvu = await db.DichVus.FindAsync(id);
            if (dichvu == null)
            {
                return HttpNotFound();
            }
            return View(dichvu);
        }

        // POST: /DichVus/Edit/5
       
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "MaDV,TenDV,MoTa,Gia")] DichVu dichvu)
        {
            if (ModelState.IsValid)
            {
                db.Entry(dichvu).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(dichvu);
        }

        // GET: /DichVus/Delete/5
        public async Task<ActionResult> Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DichVu dichvu = await db.DichVus.FindAsync(id);
            if (dichvu == null)
            {
                return HttpNotFound();
            }
            return View(dichvu);
        }

        // POST: /DichVus/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string id)
        {
            DichVu dichvu = db.DichVus.Find(id);
            db.DichVus.Remove(dichvu);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
